package de.bazinga.rotw.weapon.grenade;

import java.util.Stack;
import java.util.Vector;

import com.jme.math.Vector3f;
import com.jme.scene.Node;

import de.bazinga.rotw.person.Person;

public class GrenadeList {
	private Vector<GrenadeObject> granades = new Vector<GrenadeObject>();

	private Stack<GrenadeObject> unusedGranades = new Stack<GrenadeObject>();
	
	private static GrenadeList instance = null;
	
	private GrenadeList() {
		
	}
	
	public static GrenadeList getInstance() {
		if(instance == null) instance = new GrenadeList();
		return instance;
	}
	
	public void addGranade(GrenadeObject b) {
		granades.add(b);
	}
	
	public void removeGranade(GrenadeObject b) {
		granades.remove(b);
	}
	
	public Vector<GrenadeObject> getGranades() {
		return granades;
	}
	
	public void removeAll() {
		granades.removeAllElements();
	}

	
	
	public synchronized GrenadeObject getUnusedGranade() {
		if(unusedGranades.size() == 0)  return null;
		
		return unusedGranades.pop();
	}
	
	
	public synchronized GrenadeObject getUnusedGranade(Grenade granadeWeapon, Person person, Node rootNode, Vector3f location, Vector3f direction) {
		GrenadeObject granade = GrenadeList.getInstance().getUnusedGranade();
		
		if(granade == null) {
			granade = new GrenadeObject(granadeWeapon, person,rootNode, location, direction);
		}else{
			granade.reset(granadeWeapon, person, location, direction);
			granade.setActive(true);
		}

		return granade;
	}
	
	public synchronized void addUnusedGranade(GrenadeObject b) {
		unusedGranades.push(b);
	}
	
	public synchronized void clearUnusedGrenade() {
		unusedGranades.clear();
	}
}
